package com.powernode.driver.service.impl;


import com.powernode.driver.properties.TencentProperties;
import com.powernode.driver.service.CiService;
import com.powernode.model.vo.order.TextAuditingVo;
import com.qcloud.cos.COSClient;
import com.qcloud.cos.ClientConfig;
import com.qcloud.cos.auth.BasicCOSCredentials;
import com.qcloud.cos.auth.COSCredentials;
import com.qcloud.cos.http.HttpProtocol;
import com.qcloud.cos.model.ciModel.auditing.*;
import com.qcloud.cos.region.Region;
import jakarta.annotation.Resource;
import org.apache.commons.codec.binary.Base64;
import org.springframework.stereotype.Service;

import java.util.List;


@Service
public class CiServiceImpl implements CiService {

    @Resource
    private TencentProperties tencentProperties;


    /**
     * 文本审核
     */
    @Override
    public TextAuditingVo textAuditing(String content) {

        TextAuditingVo textAuditingVo = new TextAuditingVo();

        //获取cosClient对象
        COSClient cosClient = getCosClient();

        //构建文本请求
        TextAuditingRequest request = new TextAuditingRequest();

        request.setBucketName(tencentProperties.getBucketPrivate());

        //将内容转成base64
        byte[] bytes = Base64.encodeBase64(content.getBytes());
        String contentBase64 = new String(bytes);
        //将文本信息传入request中
        request.getInput().setContent(contentBase64);
        //全部场景
        request.getConf().setDetectType("all");

        //发送请求
        TextAuditingResponse response = cosClient.createAuditingTextJobs(request);
        AuditingJobsDetail detail = response.getJobsDetail();

        if ("success".equalsIgnoreCase(detail.getState())) {

            //获取文本审核的结果
            String result = detail.getResult();

            StringBuffer keywords = new StringBuffer();

            List<SectionInfo> sectionList = detail.getSectionList();

            for (SectionInfo info : sectionList) {
                String pornInfoKeyword = info.getPornInfo().getKeywords();
                String adsInfoKeyword = info.getAdsInfo().getKeywords();

                //判断违规关键词的内容
                if (pornInfoKeyword != null && pornInfoKeyword.length() > 0) {
                    keywords.append(pornInfoKeyword).append(",");
                }
                if (adsInfoKeyword != null && adsInfoKeyword.length() > 0) {
                    keywords.append(adsInfoKeyword).append(",");
                }
            }

            textAuditingVo.setResult(result);
            textAuditingVo.setKeywords(keywords.toString());
        }
        return textAuditingVo;
    }


    /**
     * 图片审核
     *
     * @return
     */
    @Override
    public Boolean imageAuditing(String path) {
        COSClient cosClient = getCosClient();

        //审核图片
        ImageAuditingRequest request = new ImageAuditingRequest();
        //设置bucket
        request.setBucketName(tencentProperties.getBucketPrivate());
        //设置图片路径
        request.setObjectKey(path);

        //发送审核请求
        ImageAuditingResponse response = cosClient.imageAuditing(request);

        cosClient.shutdown();

        //返回的结果 0 正常  1 违规  2 疑似
        if (!response.getPornInfo().getHitFlag().equals("0")) {
            return false;
        }

        return true;
    }


    private COSClient getCosClient() {
        COSCredentials cred = new BasicCOSCredentials(tencentProperties.getSecretId(), tencentProperties.getSecretKey());
        ClientConfig clientConfig = new ClientConfig(new Region(tencentProperties.getRegion()));
        clientConfig.setHttpProtocol(HttpProtocol.https);
        COSClient cosClient = new COSClient(cred, clientConfig);
        return cosClient;
    }
}
